<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        h2 {
            color: #333;
            margin-top: 20px;
        }
        form {
            max-width: 400px;
            width: 100%;
            padding: 20px;
            background-color: #fff;
            border-radius: 5px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
        input {
            width: 100%;
            padding: 10px;
            margin: 5px 0;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        button {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        .table-responsive {
            max-width: 800px;
            width: 100%;
            margin-top: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        .center {
            text-align: center;
        }
    </style>
</head>
<body>
<!-- 包含头部和导航栏 -->
<div th:replace="twoGroup/twoGroup.html"></div>
<h2>新增价格设置</h2>
<form id="priceSettingForm">
    <input type="number" step="0.01" id="productPrice" placeholder="产品价格" required>
    <button type="submit">添加价格设置</button>
</form>

<div class="table-responsive">
    <table class="table table-sm table-hover table-bordered">
        <thead>
        <tr>
            <th>编号</th>
            <th>产品价格</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="site : ${List}">
            <td th:text="${site.id}"></td>
            <td th:text="${site.productPrice}"></td>
            <td class="center">
                <button type='button' data-price_id='" + price.id + "' onclick='deletePrice(event)'>删除价格</button>
            </td>
        </tr>
        </tbody>
    </table>
</div>

</body>
<script src="../js/jquery-3.3.1.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script>
    $(document).ready(function() {
        // 页面加载完成后发送Ajax GET请求获取价格数据
        $.ajax({
            type: "GET",
            url: '/price',
            success: function(data) {
                // 渲染价格数据到表格中
                data.forEach(function(price) {
                    $("tbody").append(
                        "<tr>" +
                        "<td>" + price.id + "</td>" +
                        "<td>" + price.productPrice + "</td>" +
                        "<td class='center'>" +
                        "<button type='button' data-price_id='" + price.id + "' onclick='deletePrice(event)'>删除价格</button>" +
                        "</td>" +
                        "</tr>"
                    );
                });
            }
        });
    });


    $("#priceSettingForm").submit(function(event) {
        event.preventDefault();
        let datas = {
            productPrice: $("#productPrice").val(),
        };

        $.ajax({
            type: "POST",
            url: '/price/add',
            data: JSON.stringify(datas),
            contentType: "application/json",
            success: function(data) {
                alert("成功");
            }
        });
    });

    function deletePrice(event) {
        let price_id = $(event.currentTarget).data('price_id');


        $.ajax({
            type: "POST",
            url: `/price/delete/${price_id}`,
            contentType: "application/json",
            success: function() {
                alert("价格删除成功");
            },
            error: function() {
                alert("价格删除失败");
            }
        });
    }

</script>
<div th:replace="twoGroup/twoGroupmain.html"></div>
</html>